package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.interfaces.UserInfoChangeInterface;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.utils.DataCheckUtil;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ChangeEmailActivity extends BaseActivity implements UserInfoChangeInterface {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.rl_toolbar)
    RelativeLayout rlToolbar;
    @InjectView(R.id.edt_company_emali)
    EditText edtCompanyEmali;
    @InjectView(R.id.btn_ok)
    Button btnOk;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_email);
        ButterKnife.inject(this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在上传");
        ((JianzhijiaApplacation) getApplication()).addUserInfoChange(this);
        initData();
        initEvent();
    }

    private void initData() {
        edtCompanyEmali.setText(jianzhijiaApplacation.getCompanyUser().getEmail());
    }

    private void initEvent() {
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = edtCompanyEmali.getText().toString().trim();
                if(!checkData(email)){
                    return ;
                }

                progressDialog.show();
                jianzhijiaApplacation.getCompanyUser().setEmail(email);
                jianzhijiaApplacation.getCompanyUser().getAvUser().saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if(e==null){
                            progressDialog.dismiss();
                            jianzhijiaApplacation.getCompanyUser().setEmailVerified(false);
                            ToastUtils.showToast(getApplicationContext(), "修改成功");
                            jianzhijiaApplacation.updateCompanyInfo();
                            finish();
                        }else{
                            progressDialog.dismiss();
                            ToastUtils.showToast(getApplicationContext(),"修改失败 " + ExceptionUtils.getExceptionInfo(e));
                        }
                    }
                });
            }
        });
    }

    private boolean checkData(String string){

        if(string==null || string.equals("")){
            ToastUtils.showToast(getApplicationContext(),"请输入企业邮箱");
            return false;
        }else if( !DataCheckUtil.isEmail(string)){
            ToastUtils.showToast(getApplicationContext(),"您输入的邮箱格式有误");
            return false;
        }
        return true;
    }

    @Override
    public void updateCompanyInfo() {

    }

    @Override
    public void updateCompanyLogo() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((JianzhijiaApplacation) getApplication()).removeUserInfoChangeInterface(this);
    }

}
